package 宝贝复制;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by User on 2017/12/20.
 */
public class jsoup {
    public static void main(String[] args) {

        String html = "<a>#@&&(@&(OFJOFJOQJFOQJO<a>";

        String desc = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<itemRule>\n" +
                "\n" +
                "    <field id=\"update_fields\" name=\"更新字段列表\" type=\"multiCheck\">\n" +
                "        <values>\n" +
                "            <value>description</value>\n" +
                "        </values>\n" +
                "    </field>\n" +
                "\n" +
                "    <field id=\"description\" name=\"商品描述\" type=\"complex\">\n" +
                "        <complex-values>\n" +
                "            <field id=\"desc_module_user_mods\" type=\"multiComplex\">\n" +
                "                <complex-values>\n" +
                "                    <field id=\"desc_module_user_mod_name\" type=\"input\">\n" +
                "                        <value>自定义描述</value>\n" +
                "                    </field>\n" +
                "                    <field id=\"desc_module_user_mod_content\" type=\"input\">\n" +
                "                        <value>&lt;script&gt;window.TBDetail = (window.TBDetail || {});\n" +
                "                            window.TBDetail.data = {itemId:538277021308,sellerId:79742176}&lt;/script&gt;\n" +
                "\n" +
                "\n" +
                "                            &lt;p&gt;&lt;img class=\"desc_anchor\" id=\"desc-module-1\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB2PUg2XZic_eJjSZFnXXXVwVXa_!!1758965127.gif\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;p&gt;&lt;img align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i3/1758965127/TB23N1FX6gy_uJjSZKPXXaGlFXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            style=\"max-width:750.0px;\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB2JkSHX7fb_uJkSndVXXaBkpXa_!!1758965127.jpg\"\n" +
                "                            align=\"absmiddle\"/&gt;&lt;img style=\"max-width:750.0px;\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB2k9R6c_TI8KJjSsphXXcFppXa_!!1758965127.jpg\"\n" +
                "                            align=\"absmiddle\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;p&gt;&lt;img align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB2As5ic3LD8KJjSszeXXaGRpXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i2/1758965127/TB2d508c_nI8KJjSszbXXb4KFXa_!!1758965127.jpg\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;p&gt;&lt;img class=\"desc_anchor\" id=\"desc-module-2\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB2PUg2XZic_eJjSZFnXXXVwVXa_!!1758965127.gif\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;p&gt;&lt;img align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i1/1758965127/TB2j5d7c0zJ8KJjSspkXXbF7VXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i3/1758965127/TB2UoKtcZLJ8KJjy0FnXXcFDpXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i1/1758965127/TB2BYK1aGLN8KJjSZFvXXXW8VXa_!!1758965127.jpg\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;p&gt;&lt;img class=\"desc_anchor\" id=\"desc-module-3\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB2PUg2XZic_eJjSZFnXXXVwVXa_!!1758965127.gif\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;p&gt;&lt;img align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i3/1758965127/TB2x1Oic0nJ8KJjSszdXXaxuFXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i1/1758965127/TB2dH9kc22H8KJjy1zkXXXr7pXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i4/1758965127/TB274ANaoo09KJjSZFDXXb9npXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i1/1758965127/TB2VfqdcZjI8KJjSsppXXXbyVXa_!!1758965127.jpg\"/&gt;&lt;img\n" +
                "                            align=\"absmiddle\"\n" +
                "                            src=\"https://img.alicdn.com/imgextra/i1/1758965127/TB2fryicZbI8KJjy1zdXXbe1VXa_!!1758965127.jpg\"/&gt;&lt;/p&gt;\n" +
                "                            &lt;script src=\"https://g.alicdn.com/i/popshop/0.0.23/p/seemore/load.js?c\"&gt;&lt;/script&gt;\n" +
                "                        </value>\n" +
                "                    </field>\n" +
                "                    <field id=\"desc_module_user_mod_order\" type=\"input\">\n" +
                "                        <value>11</value>\n" +
                "                    </field>\n" +
                "                </complex-values>\n" +
                "            </field>\n" +
                "            <field id=\"desc_module_5_cat_mod\" type=\"complex\">\n" +
                "                <complex-values>\n" +
                "                    <field id=\"desc_module_5_cat_mod_content\" type=\"input\">\n" +
                "                        <value>&lt;p style=\"height:0px;color: #ffffff;\"&gt;商品参数模块&lt;/p&gt;</value>\n" +
                "                    </field>\n" +
                "                    <field id=\"desc_module_5_cat_mod_order\" type=\"input\">\n" +
                "                        <value>1</value>\n" +
                "                    </field>\n" +
                "                </complex-values>\n" +
                "            </field>\n" +
                "            <field id=\"desc_module_24_cat_mod\" type=\"complex\">\n" +
                "                <complex-values>\n" +
                "                    <field id=\"desc_module_24_cat_mod_content\" type=\"input\">\n" +
                "                        <value>&lt;p&gt;商品实拍模块&lt;/p&gt;</value>\n" +
                "                    </field>\n" +
                "                    <field id=\"desc_module_24_cat_mod_order\" type=\"input\">\n" +
                "                        <value>2</value>\n" +
                "                    </field>\n" +
                "                </complex-values>\n" +
                "            </field>\n" +
                "            <field id=\"desc_module_41_cat_mod\" type=\"complex\">\n" +
                "                <complex-values>\n" +
                "                    <field id=\"desc_module_41_cat_mod_content\" type=\"input\">\n" +
                "                        <value>&lt;p&gt;商品尺码表模块&lt;/p&gt;</value>\n" +
                "                    </field>\n" +
                "                    <field id=\"desc_module_41_cat_mod_order\" type=\"input\">\n" +
                "                        <value>3</value>\n" +
                "                    </field>\n" +
                "                </complex-values>\n" +
                "            </field>\n" +
                "        </complex-values>\n" +
                "    </field>\n" +
                "    \n" +
                "    \n" +
                "</itemRule>";

        Document descXml = Jsoup.parse(desc);
        // 获取排序value最小字段
        Integer minOrder = Integer.MAX_VALUE;
        Element minOrderValue = null;
        Elements values = descXml.select("field[id$=_mod_order] value");
        if (values.isEmpty()) return;
        for (Element value : values) {
            Integer order = Integer.valueOf(value.html());
            if (order < minOrder) {
                minOrder = order;
                minOrderValue = value;
            }
        }
        Element orderField = minOrderValue.parent();
        Element contentField = orderField.previousElementSibling();
        Element contentFieldValue = contentField.child(0);
        String content = contentFieldValue.html();
        String newContent = html + content;
        contentFieldValue.html(newContent);

        String newHtml = descXml.outerHtml();

        System.out.println(newHtml);

        Document parse = Jsoup.parse(newHtml);
        Elements a = parse.select("a");
        System.out.println();

    }
}
